package com.prs.crm.service.hr.impl;

import java.io.File;
import java.util.Collection;

import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.AbstractDataset;
import org.jfree.data.general.DefaultPieDataset;

import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.dao.hr.TrainDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.hr.Train;
import com.prs.crm.domain.hr.TrainItem;
import com.prs.crm.service.hr.TrainService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class TrainServiceImpl extends BaseServiceImpl<Train,TrainDao> implements TrainService{

	@Override
	public void saveTrain(Train train, File file, String fileName) {
		// TODO Auto-generated method stub
		/*Attach attach = FileUtil.getInstance().saveFile(file, fileName, "", Constrains.UPLOAD_DIC_TRAIN);
		if(attach.getSrc() != null) {
			train.setAttach(attach.getSrc());
		}*/
		
		train.setAttach(FileUtil.getInstance().createAttach(file, fileName));
		this.getDao().save(train);
		
	}

	@Override
	public Collection<Train> getTrainByPerson(Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getTrainByPerson(person);
	}

	@Override
	public Collection<?> getReportPersonTrainSource() {
		// TODO Auto-generated method stub
		return this.getDao().getReportPersonTrainSource();
	}

	@SuppressWarnings("unchecked")
	@Override
	public AbstractDataset fillDataset(Collection<?> dataList, int GraphicsType) {
		// TODO Auto-generated method stub
		AbstractDataset Dataset = null;
		if(GraphicsType == Constrains.REPORT_PIECHART)
			Dataset = new DefaultPieDataset();
		else if(GraphicsType == Constrains.REPORT_BARCHART)
			Dataset = new DefaultCategoryDataset();
		
		for(Object[] o : (Collection<Object[]>)dataList){
			String name = "";
			Number count = 0;
			for(Object value : o){
				if(value != null){
					if(value instanceof TrainItem){
						name = ((TrainItem) value).getName();
					}else if(value instanceof Number) {
						count = (Number)value;
					}
				}
			}
			if(!name.equals("")){
				if(GraphicsType == Constrains.REPORT_PIECHART)
					((DefaultPieDataset)Dataset).setValue(name, count);
				else if(GraphicsType == Constrains.REPORT_BARCHART)
					((DefaultCategoryDataset)Dataset).addValue(count, name, name);
			}
		}
		return Dataset;
	}

	@Override
	public void saveTrains(Train train, File file, String fileName) {
		// TODO Auto-generated method stub
		Attach tAttach=FileUtil.getInstance().saveFile(file, fileName, "", Constrains.UPLOAD_DIC_TRAIN);
		if(tAttach.getSrc()!=null){
			train.setTrain_file(tAttach.getSrc());
		}
		this.getDao().save(train);
	}

	

}
